﻿#if UNITY_IOS
using System.Collections.Generic;
using Unity.Notifications.iOS;

namespace Notifications.Runtime.iOS
{
    public class iOSBackgroundNotificationManager : IGameBackgroundNotificationsPlatform
    {
        private Dictionary<int, iOSBackgroundNotificationTask> _backgroundNotifications = new Dictionary<int, iOSBackgroundNotificationTask>();
        private bool _isForeground = true;
        public void Init()
        {
            
        }

        public void ScheduleBackgroundNotification(IGameBackgroundNotification gameNotification)
        {
            iOSBackgroundGameNotification notification = gameNotification as iOSBackgroundGameNotification;
            if (notification!= null)
            {
                if (notification.Id.HasValue)
                {
                    if (!_backgroundNotifications.TryGetValue(notification.Id.Value,out iOSBackgroundNotificationTask notificationTask))
                    {
                        notificationTask =new iOSBackgroundNotificationTask(notification);
                        _backgroundNotifications[notification.Id.Value] = notificationTask;
                    }
                    if (_isForeground)
                    {
                        notificationTask.Wait();  
                    }
                    else
                    {
                        notificationTask.SendNotification();
                    }
                }
            }
        }
        
        public void UpdateDateBackgroundNotification(IGameBackgroundNotification gameNotification)
        {
            if (_backgroundNotifications.TryGetValue(gameNotification.GetId(), out iOSBackgroundNotificationTask notificationTask))
            {
                iOSBackgroundGameNotification notification = gameNotification as iOSBackgroundGameNotification;
                if (notification != null)
                {
                    notificationTask.Notification = notification;
                }
            }
            else
            {
                ScheduleBackgroundNotification(gameNotification);
            }
        }

        public void CancelBackgroundNotification(int id)
        {
            if (_backgroundNotifications.TryGetValue(id,out iOSBackgroundNotificationTask notificationTask))
            {
                iOSNotificationCenter.RemoveDeliveredNotification(notificationTask.Notification.InternalNotification.Identifier);
                notificationTask.Dispose();
                _backgroundNotifications.Remove(id);
            }
        }
        public void CancelAllBackgroundNotifications()
        {
            foreach (var notificationTask in _backgroundNotifications.Values)
            {
                notificationTask.Dispose();
            }
            _backgroundNotifications.Clear();
        }

        public void OnForeground()
        {
            _isForeground = true;
            foreach (var notificationTask in _backgroundNotifications.Values)
            {
                notificationTask.ClearNotification();
            }
        }

        public void OnBackground()
        {
            _isForeground = false;
            foreach (var notificationTask in _backgroundNotifications)
            {
                notificationTask.Value.SendNotification();
            }
        }

        public IGameBackgroundNotification CreateBackgroundNotification()
        {
            return new iOSBackgroundGameNotification();
        }

        public void Dispose()
        {
            CancelAllBackgroundNotifications();
        }
        
    }
}
#endif